<?php
/**
* 컨텐츠 로직파일
* /contents/index.php
* 2011-10-26
* btslove <btslove@nextez.co.kr>
**/
/*****************************************************************************
* common
*****************************************************************************/

require_once '../common/inc.header.php';
require_once $App->_dir_lib . '/include/inc.frames.php';
require_once $App->_dir_lib . '/include/inc.menus.php';

/*****************************************************************************
* set config
*****************************************************************************/
$mid	= $App->nvl('mid', '01');
$frame	= $App->nvl('frame');
$pno	= $App->nvl('pno');


/*****************************************************************************
* set menu
*****************************************************************************/
$mids = $String->str_split($mid, 2);
if (!count($mids) || !array_key_exists($mids[0], $g_menus)) $Error->notice(NOT_FIND_MENU);
$mcurrent = & $g_menus[$mids[0]];

$mfamily[] = $mcurrent;
for($i=1;$i<=2;$i++)
{
	if ($mcurrent['pages'] || !is_array($mcurrent['childs'])) break;
	if ($mids[$i] == '')
	{
		$arr_keys = array_keys($mcurrent['childs']);
		if (count($arr_keys))
		{
			$j = 0;
			//while ($mcurrent['childs'][$arr_keys[$j]]['hidden']) $j++;
			$mid .= $arr_keys[$j];
			$mids[$i] = $arr_keys[$j];
		}
	}
	if (!array_key_exists($mids[$i], $mcurrent['childs'])) $Error->notice(NOT_FIND_MENU);
	$mcurrent = &$mcurrent['childs'][$mids[$i]];
	$mfamily[] = $mcurrent;
}
/*****************************************************************************
* set page
*****************************************************************************/
if ($mcurrent['pages'] && !$pno)
{
	if (count($mids) > count($mfamily))
	{
		$idx = count($mfamily);
		$pno = $mids[$idx];
		for ($i=$idx, $size=count($mids); $i<$size; $i++) unset($mids[$i]);
		$mid = implode('', $mids);
	}
	else
	{
		$arr_keys = array_keys($mcurrent['childs']);
		if (count($arr_keys))
		{
			$j = 0;
			while ($mcurrent['childs'][$arr_keys[$j]]['hidden']) $j++;
			$pno = $arr_keys[$j];
		}
	}
}
if ($pno)
{
	if (!$mcurrent['childs'] || !array_key_exists($pno, $mcurrent['childs'])) $Error->notice(NOT_FIND_DATA, '페이지정보');
	$pcurrent =  $mcurrent['childs'][$pno];
}


/*****************************************************************************
* set title
*****************************************************************************/
foreach ($mfamily as $v)
{
	if ($v['title']) $g_title[] = $v['title'];
}

/*****************************************************************************
* set count
*****************************************************************************/
/*
$App->class_require('Table.Counter');
$Counter = new Counter($Db);
$Counter->set($mid.$pno, addslashes(implode(' > ', $g_title)));
*/
/*****************************************************************************
* set frame
*****************************************************************************/
if (!$frame)
{
	foreach ($mfamily as $v)
	{
		if ($v['frame']) $frame = $v['frame'];
	}
}
if (!$frame) $frame = 'default';
$fcurrent = $g_frames[$frame];
/*****************************************************************************
* set contents
*****************************************************************************/
$contents = $pcurrent ? $pcurrent : $mcurrent;
if (!$contents['html'] && !$contents['logic'] && !$contents['board']) $contents = false;

/*****************************************************************************
* execution logic
*****************************************************************************/
if ($contents['logic'])
{
	if ($_SERVER['REQUEST_METHOD'] == 'GET')
	{
		if ($contents['param'])
		{
			parse_str($contents['param'], $params);
			$_GET = $Array->merge_recursive($_GET, $params);
		}
	}
	$logic = $contents['logic'];
	$is_logic_include = true;
	require $App->_dir_sub . $logic;
}
/*****************************************************************************
* execution board
*****************************************************************************/
if ($contents['board'])
{
	if ($contents['param'])
	{
		$App->class_require('Array2');
		$Array = new Array2();
		parse_str($contents['param'], $params);
		$_GET = $Array->merge_recursive($_GET, $params);
	}

	$is_board_logic_include = true;
	$_GET['ebcf_id'] = $contents['board'];

	
	include_once $App->_dir_board . '/index.php';
}



/*****************************************************************************
* print
*****************************************************************************/
// frame header
if ($fcurrent)
{
	//$g_stylesheet[] = "/css/contents.css";

	if ($fcurrent['css']) 
	{
		$css = explode(",",$fcurrent['css']);
		foreach($css as $k => $v)
			$g_stylesheet[]  = $v;
	}
	if ($fcurrent['html_header']) include_once $App->_dir_frame . $fcurrent['html_header'];
	if ($fcurrent['page_header']) include_once $App->_dir_frame . $fcurrent['page_header'];
	if ($fcurrent['paging'] && is_array($mcurrent['childs'])) include_once $App->_dir_frame . $fcurrent['paging'];
}

if ($contents)
{
	// html
	if ($contents['html'])
	{

		$html_dir = $App->_dir_html . "/sub{$mids[0]}/sub{$mids[1]}{$mids[2]}{$pno}.htm";

		
		if (file_exists($html_dir)) include_once $html_dir;
		else include_once $App->_dir_template . "/ready.htm";
	}
	// logic template
	if ($contents['logic'] && $tpl)
	{
		$tpl_path = $App->_dir_template . $tpl;
		if (file_exists($tpl_path)) include_once $App->_dir_template . $tpl;
		else $Error->notice(NOT_FIND_TPL_FILE, "tpl : " . str_replace($App->_dir_root, "", $App->_dir_template . $tpl));
	}
	// board template
	if ($contents['board'] && count($ezbbs_tpls))
	{
		foreach ($ezbbs_tpls as $tmp_tpl)
		{
			if ($tmp_tpl) include $tmp_tpl;
		}
	}
}
else
{

	include_once $App->_dir_template . "/ready.htm";
}

// frame footer
if ($fcurrent)
{
	if ($fcurrent['page_footer']) include_once $App->_dir_frame . $fcurrent['page_footer'];

	if ($fcurrent['html_footer']) include_once $App->_dir_frame . $fcurrent['html_footer'];
}

/*****************************************************************************
* include common footer
*****************************************************************************/
require_once '../common/inc.footer.php';


?>